package com.wangwen.wechatmp.wxpkg.handler;

import com.wangwen.wechatmp.dao.UserInfoDao;
import com.wangwen.wechatmp.model.UserInfo;
import com.wangwen.wechatmp.wxpkg.budiler.TextBuilder;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpCardService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * * @author Binary Wang(https://github.com/binarywang)
 */
@Component
public class SubscribeHandler extends AbstractHandler {



	@Autowired
	private UserInfoDao userInfoDao;

	@Override
	public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService weixinService,
			WxSessionManager sessionManager) throws WxErrorException {

		this.logger.info("新关注用户 OPENID: " + wxMessage.getFromUser());

//		WxMpCardService wxMpCardService =;
		// 获取微信用户基本信息 关注后发送地
		WxMpUser userWxInfo = weixinService.getUserService().userInfo(wxMessage.getFromUser());
		userWxInfo.getHeadImgUrl();
		String accessToken = weixinService.getAccessToken();
		this.logger.info("用户 accessToken: " + accessToken);

		if (userWxInfo != null) {
			//先保存用户的微信相关信息  如果是第一次扫码进来，就保存，如果之前有，不保存
			String openId = userWxInfo.getOpenId();
			userWxInfo.getUnionId();

		}else{
			return new TextBuilder().build("操作失败，请重新关注本公众号！", wxMessage, weixinService);
		}

		WxMpXmlOutMessage responseResult = null;
		try {
			responseResult = handleSpecial(wxMessage);
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
		}

		if (responseResult != null) {
			return responseResult;
		}
		String ticket = wxMessage.getTicket();

		try {
			WxMenu wxMenu = new WxMenu();
			List<WxMenuButton> wxButtons = new ArrayList<>();

			WxMenuButton button1 = new WxMenuButton();
			List<WxMenuButton> subButtons1 = new ArrayList<>();
			WxMenuButton subButton1 = new WxMenuButton();
			WxMenuButton subButton2 = new WxMenuButton();
			WxMenuButton subButton3 = new WxMenuButton();
			WxMenuButton subButton4 = new WxMenuButton();
			WxMenuButton subButton5 = new WxMenuButton();

			button1.setKey("button1");
			button1.setName("订房");
			button1.setType(WxConsts.MenuButtonType.MINIPROGRAM);
			button1.setAppId("wxd073f16e97f77d04");
			button1.setPagePath("pages/xhotel-index/xhotel-index");
			button1.setUrl("http://mindcar.cn");

//			subButton1.setKey("xcx");
//			subButton1.setName("订房小程序");
//			subButton1.setType(WxConsts.MenuButtonType.MINIPROGRAM);
//			subButton1.setAppId("wxd073f16e97f77d04");
//			subButton1.setPagePath("pages/xhotel-index/xhotel-index");
//            subButton1.setUrl("http://mindcar.cn");
//
//			subButtons1.add(subButton1);
//			button1.setSubButtons(subButtons1);


			WxMenuButton button2 = new WxMenuButton();

			List<WxMenuButton> subButtons2 = new ArrayList<>();
			WxMenuButton subButton6 = new WxMenuButton();
			WxMenuButton subButton7 = new WxMenuButton();
			button2.setKey("button2");
			button2.setName("活动");

			subButton6.setKey("wdjf");
			subButton6.setName("我的积分");
			subButton6.setType(WxConsts.MenuButtonType.VIEW);
            http://xface.mindcar.cn/public/offiaccount/?hotel_id=10000#/membership
			subButton6.setUrl("http://xface.mindcar.cn/public/offiaccount/?hotel_id="+context.get("hotelId")+"#/point");



			WxMenuButton button3 = new WxMenuButton();
			List<WxMenuButton> subButtons3 = new ArrayList<>();
			WxMenuButton button11 = new WxMenuButton();
			WxMenuButton button12 = new WxMenuButton();


			button3.setKey("appdown");
			button3.setName("会员");
//			button3.setType(WxConsts.MenuButtonType.CLICK);
			button11.setKey("hyzx");
			button11.setName("会员中心");
			button11.setType(WxConsts.MenuButtonType.VIEW);
			button11.setUrl("http://xface.mindcar.cn/public/offiaccount/?hotel_id="+context.get("hotelId")+"#/membership");

			subButtons3.add(button11);
			button3.setSubButtons(subButtons3);
			wxButtons.add(button1);
			wxButtons.add(button3);

			wxMenu.setButtons(wxButtons);
			// 设置菜单

			this.logger.info("自定义菜单JSON报文" + wxMenu.toJson());
			weixinService.getMenuService().menuCreate(wxMenu);

			WxMpXmlOutNewsMessage.Item item = new WxMpXmlOutNewsMessage.Item();
			item.setDescription("欢迎关注");
			item.setPicUrl("http://mmbiz.qpic.cn/mmbiz_jpg/YCpMv8vSQoO0CMVqAicEIyXe3ZCicY4QOO9VHFb8xXPXRpPn7ybLCEAEicuSXPWiajB0NtYATtb7OmiaECyGSXmrJSQ/0");
			item.setTitle("感谢关注，点我立即领取会员号");
			//TODO  这里可以设置关注后点击跳转到领取会员卡页面  关注即会员
			item.setUrl("http://xface.mindcar.cn/public/offiaccount/?hotel_id="+context.get("hotelId")+"#/membership");

			WxMpXmlOutNewsMessage m = WxMpXmlOutMessage.NEWS()
					.fromUser(wxMessage.getToUser())
					.toUser(wxMessage.getFromUser())
					.addArticle(item)
					.build();

			String EventKey=wxMessage.getEventKey();
			if(StringUtils.isNotEmpty(EventKey)){
			}
			this.logger.info("设置菜单:2 " + wxButtons.size());
			return m;
		} catch (Exception e) {
			e.printStackTrace();
			this.logger.error(e.getMessage(), e);
		}

		return null;
	}

	/**
	 * 处理特殊请求，比如如果是扫码进来的，可以做相应处理
	 */
	private WxMpXmlOutMessage handleSpecial(WxMpXmlMessage wxMessage) throws Exception {
		// TODO
		return null;
	}

}
